package com.mingxi.backend.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mingxi.backend.common.Result;
import com.mingxi.backend.mapper.SelectCourseMapper;
import com.mingxi.backend.mapper.StuCourseMapper;
import com.mingxi.backend.mapper.UserMapper;
import com.mingxi.backend.pojo.dto.SelectCourseDto;
import com.mingxi.backend.pojo.dto.UpdateSelectCourseDto;
import com.mingxi.backend.pojo.entity.SelectCourse;
import com.mingxi.backend.pojo.entity.User;
import com.mingxi.backend.pojo.vo.CourseStuInfo;
import com.mingxi.backend.service.SelectCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SelectCourseServiceImpl implements SelectCourseService {

    @Resource
    private SelectCourseMapper selectCourseMapper;
    @Resource
    private UserMapper userMapper;
    @Autowired
    private StuCourseMapper stuCourseMapper;


    @Override
    public Result getAvailableCourse(String username, int pageSize,int pageNumber) {
        User userInfoByName = userMapper.getUserInfoByName(username);
        if(userInfoByName == null){
            return Result.error("用户不存在");
        }
        List<SelectCourse> selectCourses = selectCourseMapper.getAll(username);
        for (SelectCourse selectCourse : selectCourses) {
            if(selectCourse.getTotal() >= selectCourse.getMaxLimit()){
                selectCourse.setIsAvailable(false);
            }
        }
        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<SelectCourse> pageInfo = new PageInfo<>(selectCourses);
        return Result.success(pageInfo);
    }

    @Override
    @Transactional
    public Result selectCourse(SelectCourseDto dto) {
        User userInfoByName = userMapper.getUserInfoByName(dto.getUsername());
        if(userInfoByName == null){
            return Result.error("用户不存在");
        }
        //TODO 待加锁
        SelectCourse selectCourse = selectCourseMapper.getById(dto.getSelectCourseId());
        if(selectCourse == null){
            return Result.error("课程不存在");
        }
        if(selectCourse.getTotal() >= selectCourse.getMaxLimit()){
            return Result.error("选课人数超过限制");
        }
        UpdateSelectCourseDto updateSelectCourseDto = new UpdateSelectCourseDto();
        updateSelectCourseDto.setSelectCourseId(dto.getSelectCourseId());
        updateSelectCourseDto.setTotal(selectCourse.getTotal() + 1);
        if(selectCourse.getTotal() >= selectCourse.getMaxLimit()){
            updateSelectCourseDto.setIsAvailable(false);
        }
        selectCourseMapper.update(updateSelectCourseDto);
        stuCourseMapper.insert(userInfoByName.getId(), dto.getSelectCourseId());
        return Result.success("选课成功！");
    }

    @Override
    public List<CourseStuInfo> getCourseStuInfo(int selectCourseId) {
        List<CourseStuInfo> courseStuInfos = stuCourseMapper.getStuInfo(selectCourseId);
        return courseStuInfos;
    }

    @Override
    public List<SelectCourse> getCourseByTeacherId(int teacherId) {
        List<SelectCourse> selectCourses = selectCourseMapper.getCourseByTeacherId(teacherId);
        return selectCourses;
    }
}
